package com.sj.purchase.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author yangrd
 * @date 2018/8/11
 **/
public class Date2StrUtils {
    private static ThreadLocal<SimpleDateFormat> formatThreadLocal = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy/MM/dd"));

    public static String str(Date date) {
        return formatter().format(date);
    }

    public static String str(Date date, String formatStr) {
        return new SimpleDateFormat(formatStr).format(date);
    }

    public static Date parse(String dateStr) {
        Date date = null;
        try {
            date = formatter().parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Date parse(String dateStr, String formatStr) {
        try {
            return new SimpleDateFormat(formatStr).parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static SimpleDateFormat formatter() {
        return formatThreadLocal.get();
    }
}
